package com.lj.oss.controller;

import com.lj.oss.service.OssService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;


/**
 * 两个接口一个上传一个下载
 */
@RestController
@RequestMapping("/oss")
@CrossOrigin
public class OssController {

    @Autowired
    private OssService ossService;

    @PostMapping("/uploadFile")
    @ApiOperation("文件上传")
    public String uploadFile(MultipartFile file){
       return ossService.upload(file);
    }

    @GetMapping("/downloadFile/{yyyy}/{MM}/{dd}/{fileName}")
    @ApiOperation("文件下载")
    public void downloadFile(@PathVariable("yyyy") String yyyy,@PathVariable("MM") String MM,
                             @PathVariable("dd") String dd,@PathVariable("fileName") String fileName,
                             HttpServletResponse response) throws IOException {
        ossService.download(yyyy+"/"+MM+"/"+dd+"/"+fileName,response);
    }

    @GetMapping("/delete/{yyyy}/{MM}/{dd}/{fileName}")
    @ApiOperation("文件删除")
    public void delete(@PathVariable("yyyy") String yyyy,@PathVariable("MM") String MM,
                       @PathVariable("dd") String dd,@PathVariable("fileName") String fileName){
        ossService.delete(yyyy+"/"+MM+"/"+dd+"/"+fileName);
    }

}
